# -*- coding: utf-8 -*-
"""
1.scrapy 携带cookies进行访问页面
"""
import scrapy


class Git1Spider(scrapy.Spider):
    name = 'git1'
    allowed_domains = ['github.com']
    start_urls = ['https://github.com/DrPandaWuDi']

    # 重写scrapy.spider中的start_request()方法，实现携带cookies
    def start_requests(self):
        url = self.start_urls[0]
        temp = '_ga=GA1.2.1286351039.1580529249; tz=Asia/Shanghai; _octo=GH1.1.1280180135.1617252703; _device_id=09e4a20995653018720e1795b12c6718; tz=Asia/Shanghai; color_mode={"color_mode":"light","light_theme":{"name":"light","color_mode":"light"},"dark_theme":{"name":"dark","color_mode":"dark"}}; user_session=zUdvcBC2MWDY8ycqail6ozi7oMSX_ceERvhqe-R2qnGksaS6; __Host-user_session_same_site=zUdvcBC2MWDY8ycqail6ozi7oMSX_ceERvhqe-R2qnGksaS6; logged_in=yes; dotcom_user=DrPandaWuDi; has_recent_activity=1; _gh_sess=1Ocv5sTZSSCuR26lmh7CXgvFlcFUw9xAecPhmauAzYXwRpe8aIjSKnaledmPyK26vBVNrAgjPw0iZdDHKnPqNKrzANmnQHG+9gRJYCKwV5E6jJymaxVUYfFDe5X53h5L7glO+N6+KI/9zNXfBvDkjlN1vyUg5Q8Ufv4hzrIlEUaUWPtkcxKlSthKSGSU255QRk1oQkK/Nntp0gmi3O0OHV1OHAdiNRK1FtFhtV83MULZFudJMLpQuw3Os7lSln61DK2fFhHzeGIKZX7ozQlv0U4j5s9mUYOHmMNesIIgV9PqKsoM7lSxO2RkM/j7iZVjVzEzwgZVowyQ+hghRyf7Oqlepr3ok1bLXEPHED6uTWHpu95I8A3jtqKvXUIBjp8FT+uR5S8A0oIkAeCF0oR/UvWJ4IPR73aLsojiMmmcg8NVeLeG0FXMEJZUWlORGSsifXrMFw==--K51rs/+IQWR0iEkz--Eedmc+xUSkuJKH+lNRJQmw=='
        cookies = {data.split("=")[0]: data.split("=")[-1] for data in temp.split(";")}
        yield scrapy.Request(
            url=url,
            cookies=cookies,
            callback=self.parse
        )

    def parse(self, response):
        print(response.xpath('/html/head/title/text()').extract_first())

